package com.example.gzysmartcity.ui.base;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewbinding.ViewBinding;

import com.example.gzysmartcity.R;
import com.example.gzysmartcity.databinding.FragmentBaseBinding;
import com.example.gzysmartcity.viewmodel.MainViewModel;

import org.jetbrains.annotations.NotNull;

import java.util.Objects;

public abstract class BaseFragment<B extends ViewBinding> extends Fragment {
    public B b;
    public FragmentBaseBinding baseBinding;
    public Boolean showBottom;
    public MainViewModel mainViewModel;

    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        baseBinding = FragmentBaseBinding.inflate(inflater, container, false);
        mainViewModel = new ViewModelProvider(requireActivity()).get(MainViewModel.class);
        b = viewBinding();
        b.getRoot().setBackgroundColor(Color.WHITE);
        if (setTitle() != null) baseBinding.title.setText(setTitle());
        else baseBinding.layBase.setVisibility(View.GONE);
        if (!showBack()) baseBinding.back.setVisibility(View.GONE);
        else baseBinding.back.setOnClickListener(v -> remove());
        showBottom = showBottom();
        if (showBottom == null) {
            showBottom = true;
            mainViewModel.showBottom(false);
        } else if (!showBottom) {
            mainViewModel.showBottom(false);
        }
        return baseBinding.getRoot();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        baseBinding = null;
        b = null;
        if (showBottom) mainViewModel.showBottom(true);
    }

    public void jump(Class<? extends Fragment> fragmentClass, Bundle bundle, String tag) {
        requireActivity().getSupportFragmentManager().beginTransaction()
                .setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right, android.R.anim.fade_in, android.R.anim.slide_out_right)
                .addToBackStack(tag)
                .add(R.id.fgMain, fragmentClass, bundle)
                .commit();
    }

    public void jumpHide(Class<? extends Fragment> fragmentClass, Bundle bundle, String tag) {
        requireActivity().getSupportFragmentManager().beginTransaction()
                .setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right, android.R.anim.fade_in, android.R.anim.slide_out_right)
                .addToBackStack(tag)
                .add(R.id.fgMain, fragmentClass, bundle,tag)
                .hide(this)
                .commit();
    }

    public void show(String tag){
        FragmentManager supportFragmentManager=requireActivity().getSupportFragmentManager();
        supportFragmentManager.beginTransaction().show(Objects.requireNonNull(supportFragmentManager.findFragmentByTag(tag))).commit();
    }



    public void remove() {
        requireActivity().getSupportFragmentManager().popBackStack();
//        requireActivity().getSupportFragmentManager().beginTransaction().remove(this).commit();
    }


    protected abstract B viewBinding();

    protected abstract String setTitle();

    protected abstract boolean showBack();

    protected abstract Boolean showBottom();

}
